package br.edu.ifpi.aula0204.collections.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import br.edu.ifpi.aula0204.collections.dao.ConnectionFactory;
import br.edu.ifpi.aula0204.collections.dao.ParticipanteDAO;
import br.edu.ifpi.aula0204.collections.entity.Participante;

public class ParticipanteJDBCDAO implements ParticipanteDAO {

	private Connection connection;

	public ParticipanteJDBCDAO() {
		this.connection = ConnectionFactory.getConnection();
	}

	@Override
	public void save(Participante entity) {
		try {
			if (entity.getId() > 0) {
				this.update(entity);
			} else {
				if (this.findByCpf(entity.getCpf()) == null) {
					this.create(entity);
				} else {
					throw new RuntimeException("CPF ja existe!");
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private void create(Participante entity) throws SQLException {
		PreparedStatement stmt = null;
		try {
			String sql = "INSERT INTO participante (cpf, nome, fone, perfil) VALUES (?, ?, ?, ?)";
			stmt = connection.prepareStatement(sql,
					Statement.RETURN_GENERATED_KEYS);

			stmt.setString(1, entity.getCpf());
			stmt.setString(2, entity.getNome());
			stmt.setString(3, entity.getFone());
			stmt.setString(4, entity.getPerfil());

			if (stmt.executeUpdate() > 0) {
				ResultSet rs = stmt.getGeneratedKeys();
				if (rs.next()) {
					entity.setId(rs.getInt(1));
				}
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			if (stmt != null) {
				stmt.close();
			}
		}
	}

	private void update(Participante entity) throws SQLException {
		PreparedStatement stmt = null;
		try {
			String sql = "UPDATE participante SET cpf = ?, nome = ?, fone = ?, perfil = ? WHERE id = ?";
			stmt = connection.prepareStatement(sql);
			stmt.setString(1, entity.getCpf());
			stmt.setString(2, entity.getNome());
			stmt.setString(3, entity.getFone());
			stmt.setString(4, entity.getPerfil());
			stmt.setInt(5, entity.getId());
			stmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			if (stmt != null) {
				stmt.close();
			}
		}
	}

	@Override
	public void delete(int id) {
		PreparedStatement stmt = null;
		try {
			String sql = "DELETE FROM participante WHERE id = ?";
			stmt = connection.prepareStatement(sql);
			stmt.setInt(1, id);
			stmt.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public Participante find(int id) {
		String sql = "SELECT * FROM participante WHERE id = ?";
		PreparedStatement stmt = null;
		Participante participante = null;
		try {
			stmt = connection.prepareStatement(sql);
			stmt.setInt(1, id);
			stmt.execute();
			ResultSet rs = stmt.getResultSet();
			if (rs.next()) {
				participante = new Participante();
				participante.setId(rs.getInt("id"));
				participante.setCpf(rs.getString("cpf"));
				participante.setNome(rs.getString("nome"));
				participante.setFone(rs.getString("fone"));
				participante.setPerfil(rs.getString("perfil"));
				return participante;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return participante;
	}

	@Override
	public List<Participante> find() {
		String sql = "SELECT * FROM participante";
		PreparedStatement stmt = null;
		List<Participante> participantes = null;
		Participante participante = null;

		try {
			stmt = connection.prepareStatement(sql);
			ResultSet result = stmt.executeQuery();
			participantes = new ArrayList<Participante>();

			while (result.next()) {
				participante = new Participante();
				participante.setId(result.getInt("id"));
				participante.setCpf(result.getString("cpf"));
				participante.setNome(result.getString("nome"));
				participante.setFone(result.getString("fone"));
				participante.setPerfil(result.getString("perfil"));
				participantes.add(participante);
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		return participantes;
	}

	@Override
	public int count() {
		String sql = "SELECT count(id) as total FROM participante";
		PreparedStatement stmt = null;
		int total = 0;

		try {
			stmt = connection.prepareStatement(sql);
			ResultSet result = stmt.executeQuery();
			if (result.next()) {
				total = result.getInt("total");
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		return total;
	}

	@Override
	public Participante findByCpf(String cpf) {
		String sql = "SELECT * FROM participante WHERE cpf = ?";
		PreparedStatement stmt = null;
		Participante participante = null;
		try {
			stmt = connection.prepareStatement(sql);
			stmt.setString(1, cpf);
			stmt.execute();
			ResultSet rs = stmt.getResultSet();
			if (rs.next()) {
				participante = new Participante();
				participante.setId(rs.getInt("id"));
				participante.setCpf(rs.getString("cpf"));
				participante.setNome(rs.getString("nome"));
				participante.setFone(rs.getString("fone"));
				participante.setPerfil(rs.getString("perfil"));
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return participante;
	}

	@Override
	public List<Participante> findByNome(String nome) {
		String sql = "SELECT * FROM participante WHERE nome LIKE ?";
		PreparedStatement stmt = null;
		List<Participante> participantes = null;
		Participante participante = null;

		try {
			stmt = connection.prepareStatement(sql);
			stmt.setString(1, "%" + nome + "%");
			ResultSet result = stmt.executeQuery();
			participantes = new ArrayList<Participante>();

			while (result.next()) {
				participante = new Participante();
				participante.setId(result.getInt("id"));
				participante.setCpf(result.getString("cpf"));
				participante.setNome(result.getString("nome"));
				participante.setFone(result.getString("fone"));
				participante.setPerfil(result.getString("perfil"));
				participantes.add(participante);
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		return participantes;
	}

}
